package com.example.assetmanagement.service.impl;

import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.Maintenance;
import com.example.assetmanagement.mapper.MaintenanceMapper;
import com.example.assetmanagement.service.MaintenanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class MaintenanceServiceImpl implements MaintenanceService {

    private final MaintenanceMapper maintenanceMapper;

    @Override
    public PageResponse<Maintenance> list(PageRequest pageRequest,
                                         Long assetId,
                                         String assetName,
                                         Integer status,
                                         String type,
                                         Integer priority,
                                         String startDate,
                                         String endDate) {
        List<Maintenance> list = maintenanceMapper.selectPage(pageRequest.getOffset(), pageRequest.getSize(), assetId, assetName, status, type, priority, startDate, endDate);
        long total = maintenanceMapper.count(assetId, assetName, status, type, priority, startDate, endDate);
        return new PageResponse<>(total, list);
    }

    @Override
    public Maintenance getById(Long id) {
        return maintenanceMapper.selectById(id);
    }

    @Override
    @Transactional
    public void add(Maintenance maintenance) {
        maintenanceMapper.insert(maintenance);
    }

    @Override
    @Transactional
    public void update(Long id, Maintenance maintenance) {
        maintenance.setId(id);
        maintenanceMapper.update(maintenance);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        maintenanceMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) return;
        maintenanceMapper.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void updateStatus(Long id, Integer status) {
        maintenanceMapper.updateStatus(id, status);
    }

    @Override
    public List<Maintenance> getByAsset(Long assetId) {
        return maintenanceMapper.selectByAssetId(assetId);
    }

    @Override
    public Map<String, Object> stats() {
        return maintenanceMapper.stats();
    }
} 